<?php
$isPopUp = true;
$isRelodable = false;

require_once("../../../pers/inc.config.php");
require_once(RGL_FS_WEB_ROOT.'/app/_inc/inc.session.php');

$sql = "SHOW TABLE STATUS";
$objDB->query($sql,'shTab');
$sFileContent = '';
while ($rect = $objDB->fetch('shTab')) {
   $tab = $rect['Name'];
	$sout  = '"'.$tab.'";"tab';
	if (substr($rect['Comment'],0,6)== "InnoDB") $aX[0] = "";
   else $aX = explode("; InnoDB free:",$rect['Comment']);     
   $commX = $aX[0];
   $sout .= '";"";"";"";"";"";"'.$commX;
	$sout .= '"'."\r\n";
	$sFileContent .= $sout;
   $adb = array();
   $sql = "SHOW FULL COLUMNS FROM $tab";
   $objDB->query($sql,'shCol');
   $sout = "$tab";
   while ($recc = $objDB->fetch('shCol')) {
     $type = $recc['Type'];
      if (substr($type,0,6)=='varbin') {
         $type = 'varchar'.substr($type,6).' binary';
      } elseif (isset($recc['Collation']) and $recc['Collation'] == 'latin1_bin') {
				if (substr($type,-7) != ' binary') {
					$type .= ' binary';
				}
      }
		$nullable = ($recc['Null'] == 'YES')?'YES':'';	
      $sout  = '"'.$tab.'";"col';
      $sout .= '";"'.$recc['Field'];
      $sout .= '";"'.$type;
      $sout .= '";"'.$nullable;
      $sout .= '";"'.$recc['Default'] ;
      $sout .= '";"'.$recc['Extra'];
      $sout .= '";"'.$recc['Comment'];
      $sout .= '"'."\r\n";
      $sFileContent .= $sout;
   }
   $sql = "SHOW INDEX FROM $tab";
   $objDB->query($sql,'shInd');
   $sout = "$tab";
   while ($recc = $objDB->fetch('shInd')) {
   	$sout = '"'.$tab.'";"ind';
      $sout .= '";"'.$recc['Key_name'];
      $sout .= '";"'.$recc['Seq_in_index'];
      $sout .= '";"'.$recc['Column_name'];
      if ($recc['Index_type'] == 'FULLTEXT') {
         $sout .= '";"'.$recc['Index_type'];
      } else {
         $sout .= '";"'.$recc['Comment'];
      }
      $sout .= '"'."\r\n";
      $sFileContent .= $sout;
   }
}
header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: private",false); 
header("Content-Type: application/csv"); 
header("Content-Transfer-Encoding:  binary"); 
header('Content-Length: '.strlen($sFileContent));
header('Content-disposition: attachment; filename="regulus_db_image.csv"');
echo $sFileContent;
exit;
